package com.hhkj.admin.student.controller;

import com.hhkj.admin.course.model.req.CourseReq;
import com.hhkj.admin.student.domain.StudentBatch;
import com.hhkj.admin.student.domain.StudentCourse;
import com.hhkj.admin.student.model.dopo.StudentBatchPo;
import com.hhkj.admin.student.model.dopo.StudentPo;
import com.hhkj.admin.student.model.req.StudentBatchReq;
import com.hhkj.admin.student.model.req.StudentCourseReq;
import com.hhkj.admin.student.model.req.StudentReq;
import com.hhkj.admin.student.model.res.StudentBatchRes;
import com.hhkj.admin.student.service.biz.StudentBatchBizService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.LogTitlePrefix;
import com.ruoyi.common.annotation.PermissionPrefix;
import com.ruoyi.common.base.biz.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学员批次管理Controller
 *
 * @author ruoyi
 * @since 2023-05-12
 */
@LogTitlePrefix("学员批次")
@PermissionPrefix("batch:batch")
@RestController
@RequestMapping("/student/batch")
public class StudentBatchController extends BaseController<StudentBatch, StudentBatchReq, StudentBatchRes, StudentBatchBizService> {
    @Autowired
    private StudentBatchBizService studentBatchBizService;

    /**
     * 分页查询学员批次管理
     */
    @Override
    public TableDataInfo page(@RequestBody StudentBatchReq studentBatchReq) {
        startPageBody(studentBatchReq);
        List<StudentBatchPo> list = studentBatchBizService.selectStudentBatchList(studentBatchReq);
        return getDataTable(list);
    }

    /**
     * 缴费
     */
    @PreAuthorize("@ss.hasPermi(this,'pay')")
    @PostMapping("/pay/batch")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult payBatch(@RequestBody StudentBatchReq studentBatchReq) {
        return studentBatchBizService.payBatch(studentBatchReq) ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改缴费情况
     */
    @PreAuthorize("@ss.hasPermi(this,'pay')")
    @PutMapping("/update/pay")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updatePay(@RequestBody StudentBatchReq studentBatchReq) {
        return studentBatchBizService.updatePay(studentBatchReq) ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 查看批次下学员列表
     */
    @PreAuthorize("@ss.hasPermi(this,'view')")
    @PostMapping("/view/student")
    public TableDataInfo listBatchStudent(@RequestBody StudentBatchReq studentBatchReq) {
        startPageBody(studentBatchReq);
        List<StudentPo> studentList = studentBatchBizService.listBatchStudent(studentBatchReq);
        return getDataTable(studentList);
    }

    /**
     * 查看批次下学员缴费总金额
     */
    @PreAuthorize("@ss.hasPermi(this,'view')")
    @GetMapping("/count/student/price/{batchId}")
    public AjaxResult countBatchStudentPrice(@PathVariable("batchId")Long batchId) {
        StudentBatchRes studentBatchRes = studentBatchBizService.countBatchStudentPrice(batchId);
        return AjaxResult.success(studentBatchRes);
    }

    /**
     * 校验科目类别
     */
    @PreAuthorize("@ss.hasPermi(this,'bankType')")
    @PostMapping("/verify/course")
    public AjaxResult verifyCourseCategory(@RequestBody CourseReq course) {
        return studentBatchBizService.verifyCourseCategory(course) ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量设置题库类型
     */
    @PreAuthorize("@ss.hasPermi(this,'bankType')")
    @PostMapping("/bank/type")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult setBatchBankType(@RequestBody StudentCourseReq studentCourseReq) {
        studentBatchBizService.supplyBatchBankType(studentCourseReq);
        return AjaxResult.success();
    }

    /**
     * 批量审核学员
     */
    @PreAuthorize("@ss.hasPermi(this,'checkStudent')")
    @PostMapping("/check/student")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult checkBatchStudent(@RequestBody StudentReq studentReq) {
        studentBatchBizService.checkBatchStudent(studentReq);
        return AjaxResult.success();
    }

    /**
     * 批量设置到期日期
     */
    @PreAuthorize("@ss.hasPermi(this,'linedate')")
    @PostMapping("/set/lineDate")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult setBatchStudentLineDate(@RequestBody StudentCourseReq studentCourseReq) {
        studentBatchBizService.supplyBatchLineDate(studentCourseReq);
        return AjaxResult.success();
    }

    /**
     * 批量设置学员单价
     */
    @PreAuthorize("@ss.hasPermi(this,'unitprice')")
    @PostMapping("/set/unit/price")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult setBatchStudentUnitPrice(@RequestBody StudentCourseReq studentCourseReq) {
        studentBatchBizService.supplyBatchStudentUnitPrice(studentCourseReq);
        return AjaxResult.success();
    }

    /**
     * 导出批次下全部学员excl
     */
    @PreAuthorize("@ss.hasPermi(this,'export')")
    @Log(title = "导出批次学员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody StudentBatchReq studentBatchReq) {
        StudentBatch studentBatchExcl = studentBatchBizService.exportStudent(studentBatchReq);
        ExcelUtils.downLoadExcel(studentBatchExcl.getBatchNum() + "学员信息", response, studentBatchExcl.getWorkbook());
    }


    /**
     * 删除学员批次管理
     */
    @PreAuthorize("@ss.hasPermi(this,'remove')")
    @Log(title = "学员批次管理", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@RequestBody StudentBatchReq studentBatchReq) {
        List<Long> idList = studentBatchReq.getIdList();
        return toAjax(studentBatchBizService.removeStudentBatchByIdList(idList));
    }

    /**
     * 解绑微信
     *
     * @param studentBatchReq 传参
     */
    @PreAuthorize("@ss.hasPermi(this,'bind')")
    @Log(title = "解绑微信", businessType = BusinessType.CLEAN)
    @PostMapping("/cancel/bind")
    public AjaxResult cancelBind(@RequestBody StudentBatchReq studentBatchReq) {
        return studentBatchBizService.cancelBind(studentBatchReq) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

//    /**
//     * 查询学员批次管理列表
//     */
//    @PreAuthorize("@ss.hasPermi('batch:batch:list')")
//    @PostMapping("/list")
//    public TableDataInfo list(@Validated(VList.class) @RequestBody StudentBatch studentBatch) {
//        startPage();
//        List<StudentBatch> list = studentBatchBizService.selectStudentBatchList(studentBatch);
//        return getDataTable(list);
//    }
}
